﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using secondhand.L2busslogic;
using secondhand.L1database;
using secondhand.L3object;
using System.IO;

namespace secondhand.L4view
{
    public partial class saler_alert : System.Web.UI.Page
    {
        public static bool has_no_file = false;
        public static string ID;
        public static string salerName;
        protected void Page_Load(object sender, EventArgs e)
        {
            Image1.ImageUrl = "~/images/error.png";
            Image2.ImageUrl = "~/images/error.png";
            Label5.Visible = false;
            FileUpload1.Visible = false;
            Image2.Visible = false;
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('商品名称不能为空 ');</script>");
                return;
            }
            goodsOB mydd = new goodsOB();
            mydd = goodsBL.Getwordsingle1(Session["salerName"].ToString(), TextBox1.Text);
            if (mydd.goods_name.Trim() == "访问数据库不成功！")
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('没有该商品 ');</script>");
                return;
            }
            ID = mydd.goods_ID;
            TextBox2.Text = mydd.goods_type;
            TextBox3.Text = mydd.goods_price;
            TextBox4.Text = mydd.goods_describe;
            Image1.ImageUrl = mydd.goods_picture;
            salerName = mydd.saler_name;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (TextBox1.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('商品名称不能为空 ');</script>");
                return;
            }
            if (TextBox2.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('商品类型不能为空 ');</script>");
                return;
            }
            if (TextBox3.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('商品价格不能为空 ');</script>");
                return;
            }
            if (TextBox4.Text.Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('商品描述不能为空 ');</script>");
                return;
            }
            if (FileUpload1.HasFile)
            {
                string photoName = Path.GetFileName(FileUpload1.PostedFile.FileName);
                int i = photoName.LastIndexOf("."); //取得文件名中最后一个"."的索引 
                string newext = photoName.Substring(i); //获取文件扩展名
                if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
                {
                    Response.Write("文件格式不正确!");
                    Response.End();
                }
                string fileType = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
                string path = Server.MapPath("~/") + "\\L4View\\image\\" + photoName;
                FileUpload1.SaveAs(path);
            }
            has_no_file = FileUpload1.HasFile;
            goodsOB myword = new goodsOB();

            myword.goods_ID = ID;
            myword.goods_name = TextBox1.Text;
            myword.goods_type = TextBox2.Text;
            myword.goods_price = TextBox3.Text;
            myword.saler_name = salerName;
            myword.sale_time = DateTime.Now.ToString();
            myword.goods_describe = TextBox4.Text;
            myword.master_check = "待审核";
            myword.goods_picture = Image1.ImageUrl;

            if (has_no_file == true)
            {
                myword.goods_picture = "~/L4View/image/" + Path.GetFileName(FileUpload1.PostedFile.FileName);
            }
            if (!goodsBL.Savemark(myword))
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('修改不成功 ');</script>");
                return;
            }
            else
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('修改成功 ');</script>");
                TextBox1.Text = "";
                TextBox2.Text = "";
                TextBox3.Text = "";
                TextBox4.Text = "";
                return;
            }
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Label5.Visible = true;
            FileUpload1.Visible = true;
            Image2.Visible = true;
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}